package com.baseweb.webadmin.core.model.dao.impl;

import com.baseweb.webadmin.core.constants.*;
import com.baseweb.webadmin.core.model.bvo.ByUserOrderDetailInfoBvo;
import com.baseweb.webadmin.core.model.bvo.ByUserOrderEvaluatvo;
import com.baseweb.webadmin.core.model.bvo.ByUserOrderInfoBvo;
import com.baseweb.webadmin.core.model.dao.ByCoinsLogDao;
import com.baseweb.webadmin.core.model.dao.UserOrderDao;
import com.baseweb.webadmin.core.model.mapper.*;
import com.baseweb.webadmin.core.model.pojo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.tools.ant.taskdefs.condition.Or;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserOrderDaoImpl extends SqlSessionDaoSupport implements UserOrderDao {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ByUserOrderMapper byUserOrderMapper;

    @Autowired
    ByUserOrderDetailMapper byUserOrderDetailMapper;

    @Autowired
    ByUserBizScheduleMapper byUserBizScheduleMapper;

    @Autowired
    ByUserBatchPayOrderMapper byUserBatchPayOrderMapper;

    @Override
    public List<ByUserOrder> selectListByPage(List<Integer> userIds,Integer status, Integer payStatus, Integer start, Integer offset) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(userIds != null && userIds.size() > 0){
            criteria.andUserIdIn(userIds);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if (payStatus != null){
            criteria.andPayStatusEqualTo(payStatus);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserOrderMapper.selectByExample(example);
    }

    @Override
    public Integer selectListCount(List<Integer> userIds,Integer status, Integer payStatus) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(userIds != null && userIds.size() > 0){
            criteria.andUserIdIn(userIds);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if (payStatus != null){
            criteria.andPayStatusEqualTo(payStatus);
        }
        return byUserOrderMapper.countByExample(example);
    }

    @Override
    public Integer insertByUserBatchPayOrder(ByUserBatchPayOrder byUserBatchPayOrder) {
        if (byUserBatchPayOrder.getId() !=null && byUserBatchPayOrder.getId() > 0){
            return byUserBatchPayOrderMapper.updateByPrimaryKey(byUserBatchPayOrder);
        }else {
            return byUserBatchPayOrderMapper.insert(byUserBatchPayOrder);
        }
    }

    @Override
    public ByUserBatchPayOrder findByUserBatchPayOrder(Integer id) {
        return byUserBatchPayOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public ByUserBatchPayOrder findByUserBatchPayOrderByNum(String batchNum) {
        ByUserBatchPayOrderExample example = new ByUserBatchPayOrderExample();
        ByUserBatchPayOrderExample.Criteria criteria = example.createCriteria();
        if(batchNum != null){
            criteria.andBatchNumEqualTo(batchNum);
            List<ByUserBatchPayOrder> result = byUserBatchPayOrderMapper.selectByExample(example);
            if (result != null && result.size() > 0){
                return result.get(0);
            }
        }
        return null;
    }

    @Override
    public List<ByUserOrder> selectOrderListByBizUser(Integer userId, Integer bizUserId, Integer status, Integer payStatus, Integer start, Integer offset) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(bizUserId != null){
            criteria.andBizUserIdEqualTo(bizUserId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if (payStatus != null){
            criteria.andPayStatusEqualTo(payStatus);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserOrderMapper.selectByExample(example);
    }
    @Override
    public List<ByUserOrderEvaluatvo> selectOrderListByBizUserOfEvaluation(ByUserOrder order, Integer start, Integer offset){
//        ByUserOrderExample example = new ByUserOrderExample();
//        ByUserOrderExample.Criteria criteria = example.createCriteria();
        /*if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(bizUserId != null){
            criteria.andBizUserIdEqualTo(bizUserId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if (payStatus != null){
            criteria.andPayStatusEqualTo(payStatus);
        }*/
//        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset)); sumPriceFromOrder
        return byUserOrderMapper.selectOrderEvaluatvoListByPage(order, start, offset);
    }
    @Override
    public List<ByUserOrderInfoBvo> selectOrderListByPage(Integer userId,String area,Integer guserId, Integer bizUserId, Integer status, Integer payStatus,String startTime,String endTime,ByUserOrderInfoBvo user,String professionName, Integer start, Integer offset) {
        return byUserOrderMapper.selectOrderListByPage(userId,area,guserId,bizUserId,status,payStatus,startTime,endTime,user,professionName,start,offset);
    }

    @Override
    public ByUserOrderInfoBvo sumPriceFromOrder(Integer userId,String area,Integer guserId, Integer bizUserId, Integer status, Integer payStatus,String startTime,String endTime,ByUserOrderInfoBvo user,String professionName, Integer start, Integer offset) {
        return byUserOrderMapper.sumPriceFromOrder(userId,area,guserId,bizUserId,status,payStatus,startTime,endTime,user,professionName,start,offset);
    }

    @Override
    public Integer selectOrderListByBizUserCount(Integer userId,String area,Integer guserId, Integer bizUserId, Integer status, Integer payStatus,String startTime,String endTime,String professionName,ByUserOrderInfoBvo order) {

        return byUserOrderMapper.selectOrderListByBizUserCount(userId,area,guserId,bizUserId,status,payStatus,startTime,endTime,professionName,order);
    }
    @Override
    public Integer selectOrderListByBizUserCountzOfEvalution(ByUserOrder user){
        return byUserOrderMapper.selectOrderEvaluatvoListTotal(user);
    }
    @Override
    public Integer insertByUserOrder(ByUserOrder byUserOrder) {
        if (byUserOrder.getId() !=null && byUserOrder.getId() > 0){
            return byUserOrderMapper.updateByPrimaryKey(byUserOrder);
        }else {
            return byUserOrderMapper.insert(byUserOrder);
        }
    }

    @Override
    public ByUserOrder findByUserOrder(Integer id) {
        return byUserOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserOrder> findByUserOrderList(List<Integer> ids) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(ids != null && ids.size() > 0){
            criteria.andIdIn(ids);
            return byUserOrderMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<ByUserOrder> selectAllOrderListByPage(Integer userId, Integer start, Integer offset) {
        Map map = new HashMap();
        map.put("startCount", start);
        map.put("pageSize", offset);
        map.put("user_id", userId);
        map.put("status", OrderStatus.VERIFY_ORDER.getCode());
        return this.getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserOrderDao.selectAllOrderListByPage", map);
    }

    @Override
    public Integer selectAllOrderListCount(Integer userId) {
        Map map = new HashMap();
        map.put("user_id", userId);
        map.put("status", OrderStatus.BIZ_RECEIVE_ORDER.getCode());
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserOrderDao.selectAllOrderListCount", map);
    }

    @Override
    public Integer insertByUserOrderDetail(ByUserOrderDetail byUserOrderDetail) {
        if (byUserOrderDetail.getId() !=null && byUserOrderDetail.getId() > 0){
            return byUserOrderDetailMapper.updateByPrimaryKey(byUserOrderDetail);
        }else {
            return byUserOrderDetailMapper.insert(byUserOrderDetail);
        }
    }

    @Override
    public ByUserOrderDetail findByUserOrderDetail(Integer id) {
        return byUserOrderDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserOrderDetail> selectOrderDetailListByPage(Integer userId, Integer orderId, Integer start, Integer offset) {
        ByUserOrderDetailExample example = new ByUserOrderDetailExample();
        ByUserOrderDetailExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(orderId != null){
            criteria.andOrderIdEqualTo(orderId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserOrderDetailMapper.selectByExample(example);
    }

    @Override
    public List<ByUserOrderDetailInfoBvo> selectOrderDetailListByHtmlPage(Integer userId, Integer orderId, Integer start, Integer offset) {
        return byUserOrderDetailMapper.selectOrderDetailListByHtmlPage(userId,orderId,start,offset);
    }
    @Override
    public Integer selectOrderDetailListCount(Integer userId, Integer orderId) {
        ByUserOrderDetailExample example = new ByUserOrderDetailExample();
        ByUserOrderDetailExample.Criteria criteria = example.createCriteria();
//        if(userId != null){
//            criteria.andUserIdEqualTo(userId);
//        }
        if(orderId != null){
            criteria.andOrderIdEqualTo(orderId);
        }else{
            criteria.andUserIdEqualTo(userId);
        }
        return byUserOrderDetailMapper.countByExample(example);
    }

    @Override
    public Integer insertSchedule(ByUserBizSchedule byUserBizSchedule) {
        if (byUserBizSchedule.getId() != null && byUserBizSchedule.getId() > 0){
            return byUserBizScheduleMapper.updateByPrimaryKey(byUserBizSchedule);
        } else {
            ByUserBizScheduleExample example = new ByUserBizScheduleExample();
            ByUserBizScheduleExample.Criteria criteria = example.createCriteria();

            criteria.andUserIdEqualTo(byUserBizSchedule.getUserId());
            criteria.andScheduleTypeEqualTo(byUserBizSchedule.getScheduleType());
            criteria.andScheduleDateEqualTo(byUserBizSchedule.getScheduleDate());

            List<ByUserBizSchedule> result = byUserBizScheduleMapper.selectByExample(example);
            if (result == null || result.size() < 1) {
                return byUserBizScheduleMapper.insert(byUserBizSchedule);
            }else{
                byUserBizSchedule.setId(result.get(0).getId());
                byUserBizSchedule.setStatus(1);
                return byUserBizScheduleMapper.updateByPrimaryKey(byUserBizSchedule);
            }
        }
    }

    @Override
    public ByUserBizSchedule findSchedule(Integer id) {
        return byUserBizScheduleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteSchedule(Integer id) {
        return byUserBizScheduleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteSchedule(Integer type, DateTime dateTime, Integer userId) {
        ByUserBizSchedule byUserBizSchedule = new ByUserBizSchedule();
        byUserBizSchedule.setScheduleType(type);
        byUserBizSchedule.setScheduleDate(dateTime);
        byUserBizSchedule.setUserId(userId);
        Integer id = byUserBizScheduleMapper.selectIdByConditions(byUserBizSchedule);
        if (id != null && id != 0) {
            byUserBizSchedule.setId(id);
            byUserBizSchedule.setStatus(-1);
            return byUserBizScheduleMapper.updateByPrimaryKey(byUserBizSchedule);
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteScheduleByOrderId(Integer orderId) {
        List<ByUserOrderDetail> byUserOrderDetails = selectOrderDetailListByPage(null, orderId, 0, Integer.MAX_VALUE);
        if (byUserOrderDetails != null && byUserOrderDetails.size() > 0){
            for (ByUserOrderDetail current : byUserOrderDetails){
                Integer userId = current.getBizUserId();
                DateTime scheduleDate = current.getScheduleDate();
                Integer scheduleType = current.getScheduleType();
                Integer orderBy = current.getUserId();

                ByUserBizScheduleExample example = new ByUserBizScheduleExample();
                ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
                if(userId != null && scheduleDate !=null && scheduleType != null && orderBy != null){
                    criteria.andUserIdEqualTo(userId);
                    criteria.andScheduleDateEqualTo(scheduleDate);
                    criteria.andScheduleTypeEqualTo(scheduleType);
                    criteria.andOrderByEqualTo(orderBy);

                    criteria.andStatusEqualTo(BaseState.NORMAL.getCode());
                    byUserBizScheduleMapper.deleteByExample(example);
                }

            }


        }
        return 1;
    }

    @Override
    public List<ByUserBizSchedule> selectScheduleListByPage(Integer userId, Integer status, Integer start, Integer offset) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizScheduleMapper.selectByExample(example);
    }

    @Override
    public Integer selectScheduleListCount(Integer userId, Integer status) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        return byUserBizScheduleMapper.countByExample(example);
    }

    @Override
    public List<ByUserBizSchedule> selectScheduleRemindList(Integer userId, Integer status, DateTime startTime, DateTime endTime) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        if (startTime != null){
            criteria.andCreateTimeGreaterThan(startTime);
        }
        if (endTime != null){
            criteria.andCreateTimeLessThanOrEqualTo(endTime);
        }
        example.setOrderByClause(" id desc ");
        return byUserBizScheduleMapper.selectByExample(example);
    }

    @Override
    public List<ByUserBizSchedule> selectScheduleListByPage(Integer userId, Integer status, DateTime startTime, Integer start, Integer offset) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        if(startTime != null) {
            criteria.andScheduleDateEqualTo(startTime);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizScheduleMapper.selectByExample(example);
    }

    @Override
    public List<ByUserBizSchedule> selectScheduleListByPage(Integer userId, Integer status, DateTime startTime, DateTime endTime, Integer start, Integer offset) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        if(startTime != null) {
            criteria.andScheduleDateGreaterThanOrEqualTo(startTime);
        }
        if(endTime != null) {
            criteria.andScheduleDateLessThan(endTime);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserBizScheduleMapper.selectByExample(example);
    }

    @Override
    public Integer selectScheduleListCount(Integer userId, Integer status, DateTime startTime, DateTime endTime) {
        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        if(startTime != null) {
            criteria.andScheduleDateGreaterThanOrEqualTo(startTime);
        }
        if(endTime != null) {
            criteria.andScheduleDateLessThan(endTime);
        }
        return byUserBizScheduleMapper.countByExample(example);
    }

    @Override
    public boolean isScheduled(Integer userId, Integer status, DateTime scheduleTime, Integer dinnerType) {
        DateTime startTime = new DateTime(scheduleTime.getYear(), scheduleTime.getMonthOfYear(), scheduleTime.getDayOfMonth(), 0, 0, 0);
        DateTime endTime = startTime.plusDays(1);

        ByUserBizScheduleExample example = new ByUserBizScheduleExample();
        ByUserBizScheduleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null) {
            criteria.andStatusEqualTo(status);
        }
        if(dinnerType != null) {
            criteria.andScheduleTypeEqualTo(dinnerType);
//            if (dinnerType.equalsIgnoreCase(ScheduleType.DINNER.getName())){
//                criteria.andScheduleTypeEqualTo(ScheduleType.DINNER.getCode());
//            }else {
//                criteria.andScheduleTypeEqualTo(ScheduleType.LUNCH.getCode());
//            }
        }
        if(startTime != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(startTime);
        }
        if(endTime != null) {
            criteria.andCreateTimeLessThan(endTime);
        }

        List<ByUserBizSchedule> byUserBizSchedules = byUserBizScheduleMapper.selectByExample(example);

        if (byUserBizSchedules != null && byUserBizSchedules.size() > 0){
            return true;
        }

        return false;
    }

    @Override
    public List<ByUserOrder> selectBizOrderListByPage(List<Integer> userIds, Integer status, Integer start, Integer offset) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(userIds != null){
            criteria.andBizUserIdIn(userIds);
        }
        if(status != null){
            criteria.andStatusGreaterThanOrEqualTo(status);
        }
        criteria.andPayStatusNotEqualTo(OrderPayStatus.NOT_PAY.getCode()); //只显示已付款的订单
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserOrderMapper.selectByExample(example);
    }

    @Override
    public Integer selectBizOrderListCount(List<Integer> userIds, Integer status) {
        ByUserOrderExample example = new ByUserOrderExample();
        ByUserOrderExample.Criteria criteria = example.createCriteria();
        if(userIds != null){
            criteria.andBizUserIdIn(userIds);
        }
        if(status != null){
            criteria.andStatusGreaterThanOrEqualTo(status);
        }
        criteria.andPayStatusNotEqualTo(OrderPayStatus.NOT_PAY.getCode()); //只显示已付款的订单
        return byUserOrderMapper.countByExample(example);
    }

    @Override
    public Integer countOrderDiscuss(Integer id) {
        return byUserOrderMapper.countOrderDiscuss(id);
    }

    @Override
    public List<ByUserOrder> selectOrderByProductId(Integer id) {
        return byUserOrderMapper.selectOrderByProductId(id);
    }

    @Override
    public int selectScheduleByUserId(int userId, String scheduleDate, String scheduleType) {
        return byUserOrderMapper.selectScheduleByUserId(userId, scheduleDate, scheduleType);
    }

    @Override
    public int selectOrderUnfinished(int userId, String scheduleDate, String scheduleType) {
        return byUserOrderMapper.selectOrderUnfinished(userId, scheduleDate, scheduleType);
    }
}
